package yl.hs.bmipfull.service.vehiclemanage.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.vehiclemanage.IRosteringChangeDao;
import yl.hs.bmipfull.daos.vehiclemanage.ITruckInfoDao;
import yl.hs.bmipfull.pojo.vehiclemanage.RosteringChangeEntity;
import yl.hs.bmipfull.service.vehiclemanage.IRosteringChangeService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.SubmitModel;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 韩纪旺
 * @apiNote 车辆排班变更记录表服务层接口实现
 * @since 2022年09月14日 17时09分51秒 <br/>
 */
@Service
public class RosteringChangeServiceImpl extends VehicleBaseService implements IRosteringChangeService {

    @Autowired
    IRosteringChangeDao dao;

    @Autowired
    ITruckInfoDao infoDao;

    @Override
    public List<RosteringChangeEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(RosteringChangeEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<RosteringChangeEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(RosteringChangeEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<RosteringChangeEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public RosteringChangeEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saverostering_change(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        RosteringChangeEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new RosteringChangeEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(RosteringChangeEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入车辆排班变更记录表信息", Tl.toJson(entity), RosteringChangeEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新车辆排班变更记录表信息", Tl.toJson(entity), RosteringChangeEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer changeStateBySN(String sn, short state) {
        Integer result = dao.changeStateBySN(sn.split(","), state);
        if (result > 0) {
            if (state == DataState.DELETED.code) {
                writeLog(ActionType.DELETE, sn, RosteringChangeEntity.class);
            } else {
                writeLog(ActionType.UPDATE, sn, RosteringChangeEntity.class);

            }
        }
        return result;
    }


   @Override
   public PageResult<RosteringChangeEntity> bulletinBoardInformation(PageQuery pageQuery, int rowNum, int page) throws ParseException {
       pageQuery.checkSortSides(RosteringChangeEntity.class);
       pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
       pageQuery.getQuery().put("pageSize", rowNum);
       PageResult<RosteringChangeEntity> result = new PageResult(rowNum);
       result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
       result.setPage(page);
       List<RosteringChangeEntity> list1 = dao.getRosteringChangeTime();
       var bulletin = infoDao.getBulletinBoardInformation();
       for (RosteringChangeEntity informationModel : bulletin) {
          var entity = dao.getRosteringChange(informationModel.getSn());

           if(entity!=null){
               informationModel.setRosteringChangeTime(entity.getRosteringChangeTime());
               informationModel.setRemark(entity.getRemark());
               informationModel.setDriverSN(entity.getDriverSN());
           }
       }
       result.setData(bulletin);
       return result;
   }
    /**
     * 更换司机
     *
     * @param
     * @return
     */
    @Override
    public Integer changeDriver(Map<String, Object> model, String sn) throws Exception {
         RosteringChangeEntity rosteringChange = new RosteringChangeEntity();
         Tl.getChangeLog(rosteringChange, model);
        Date date=new Date();
        SimpleDateFormat mat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = mat.format(date);
        Date da = mat.parse(s);
        rosteringChange.setRosteringChangeTime(da);
        rosteringChange.setChangeRecord(rosteringChange.getOldDriverName()+ s + "将车辆交接给" + rosteringChange.getDriverName());
        rosteringChange.setSn(queryNewSN(RosteringChangeEntity.class));
        Integer result = dao.insertNew(rosteringChange);
        if(result>0){
            var truckInfo=infoDao.findBySN(rosteringChange.getTruckSN());
            if (truckInfo!=null){
                truckInfo.setDriverSN(rosteringChange.getDriverSN());
                truckInfo.setDriverName(rosteringChange.getDriverName());
                truckInfo.setPYFieldValue();
                infoDao.update(truckInfo);
            }
        }
        return result;
    }

    @Override
    public Integer getJudpe(SubmitModel submitModel) throws Exception {
            Integer result = 0;
            RosteringChangeEntity rosteringChange=new RosteringChangeEntity();
             Tl.getChangeLog(rosteringChange, submitModel.getData());
            String driverSN = rosteringChange.getDriverSN();
            result=infoDao.getJudge(driverSN);
            return result;
    }
}